<?php
   class Cart extends MY_Controller{
	   public function __construct(){
		   parent::__construct();
		   $this->load->helper("url");
		   $this->load->library('cart');
		   $this->load->helper('form');
		   $this->load->model("model_cart");
	   }
	   public function index(){
		   $this->load->view("homes/cart"); 
	   }
	   public function add(){
		   if(!$_POST){
			   die("false");
		   }
		   $rooms 	= $this->input->post("rooms");
		   $adults 	= $this->input->post("adults");
		   $child   = $this->input->post("child");
		   $infants = $this->input->post("infants");
		   $price   = $this->input->post("price");
		   $ship    = $this->input->post("ship");
		   $title   = $this->input->post("title");
		   $roomid  = $this->input->post("roomid");
		   $img     = $this->input->post("image");
		   $getshop = $this->cart->contents();
			$shop = array(
               'id'      => $roomid,
               'qty'     => 1,
               'price'   => $price,
               'name'    => $title,
               'options' => array(
			   	    'rooms'  => $rooms,
					'image'  => $img,
			   		'adults'  => $adults,
					'child'   => $child,
					'infants' => $infants,
					'ship'    => $ship
				)
            );
			$this->cart->insert($shop);
	   }
	   public function view(){
		  $data['listships'] = $this->listships(); 
		  $data['ship_budget'] 		= $this->type_ship(1);
		  $data['ship_superior'] 	= $this->type_ship(2);
		  $data['ship_deluxe'] 		= $this->type_ship(3);
		  $data['ship_luxury'] 		= $this->type_ship(4);
		  $data['ship_premium'] 	= $this->type_ship(5);
		  $data['ship_charter'] 	= $this->type_ship(6);
		  $data['random']    = $this->rand_ship();
		  $data['config']    = $this->config();
		  $data['cart'] = $this->cart->contents();
		  $cart = $this->cart->contents();
		  $abc = array();
		  foreach($cart as $items){
			  $abc[] = $this->model_cart->getservice($items['options']['ship']); 
		  }
		  $data['listall'] = array("cart" => $cart,"services" => $abc);
		  //$this->debug($data['listall']);
		  $data['title'] = "Your cart";
		  $this->load->view("cart/layout",$data);
	   }
	   public function updatecart(){
			for ($i=1;$i<=$this->cart->total_items();$i++){
		        $item = $this->input->post($i);
		        $data = array(
		                'rowid'    => $item['rowid'],
		                'qty'    => $item['qty']
				);
		        $this->cart->update($data);
			}
			redirect(base_url().'home/cart/view','refresh');		
		
		}
	   public function del(){
		  	$getshop=$this->cart->contents();
			foreach($getshop as $item){
				if($item['id'] == $this->uri->segment(4)){
					$data['rowid']=$item['rowid'];
					$data['qty'] = 0;
			        $this->cart->update($data);
					break;
				}
			}
			redirect(base_url().'your-cart#yourcart','refresh');	
	   }
	   public function emptycart(){
		   $this->cart->destroy();
		   redirect(base_url().'your-cart#yourcart','refresh');	
	   }
   }